package com.lw.sbdemo2.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lw.sbdemo2.common.exception.ApiException;
import com.lw.sbdemo2.common.exception.ResponseCodeEnum;

import java.lang.reflect.Field;
import java.util.Map;

import static com.lw.sbdemo2.common.utils.ValidUtil.isEmptyOrNull;

/**
 * <p>标题: API入参校验类</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2016</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: yt
 * @date 2016/9/28
 */
public class ParameterHelper {
    /**
     * 校验JavaBean中的字段不能为空，为空则抛异常
     * @param bean JavaBean实例
     * @param fields 字段数组
     */
    public static void checkFields(Object bean, String... fields) {
        Class cls = bean.getClass();
        for (String fieldName : fields) {
            try {
                Field field = getDeclaredField(cls, fieldName);
                if (null == field) {
                    throw new ApiException(ResponseCodeEnum.EX_PARAM, fieldName);
                }
                field.setAccessible(true);
                if (isEmptyOrNull(field.get(bean))) {
                    throw new ApiException(ResponseCodeEnum.EX_PARAM, fieldName);
                }
            } catch (IllegalAccessException e) {
                // unreachable
            }
        }
    }

    /**
     * 循环向上转型，获取对象的declaredField
     * @param clazz 子类类型
     * @param fieldName 父类中的属性名
     * @return 父类中的属性对象
     */
    public static Field getDeclaredField(Class clazz, String fieldName) {
        Field field = null;
        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            try {
                field = clazz.getDeclaredField(fieldName);
                return field;
            } catch (Exception e) {
            }
        }
        return field;
    }

    /**
     * 校验Map中的key不为空，为空则抛异常
     * @param map Map实例
     * @param keys key数组
     */
    public static void checkKeys(Map map, String... keys) {
        for (String key : keys) {
            if (!map.containsKey(key)) {
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
            if (isEmptyOrNull(map.get(key))) {
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
        }
    }

    /**
     * 验证入参节点不能为空、节点值不能为空
     * @param para 入参json
     * @param keys 要验证的节点数组
     * @throws Exception
     */
    public static Map<String, String> checkKey(String para, String... keys) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, String> map = (Map<String, String>) JSON.parse(para);
        for (String key : keys) {
            if (null == map.get(key)) {
                Object[] params = {key};
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
            if (isEmptyOrNull(map.get(key))) {
                Object[] params = {key};
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
        }
        return map;
    }

    /**
     * 验证节点不能为空
     * @param para 入参json
     * @param keys 节点数组
     * @throws Exception
     */
    public static Map<String, String> checkKeyNotNull(String para, String... keys) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, String> map = (Map<String, String>) JSON.parse(para);
        for (String key : keys) {
            if (null == map.get(key)) {
                Object[] params = {key};
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
        }
        return map;
    }

    /**
     * 验证数组节点及内容不能为空
     * @param para 入参json
     * @param keys 节点数组
     * @throws Exception
     */
    public static Map<String, JSONArray> checkArrayNotNull(String para, String... keys) throws Exception {
        @SuppressWarnings("unchecked")
        Map<String, JSONArray> map = (Map<String, JSONArray>) JSON.parse(para);
        for (String key : keys) {
            if (null == map.get(key)) {
                Object[] params = {key};
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
            JSONArray array = map.get(key);
            if (array.size() == 0) {
                Object[] params = {key};
                throw new ApiException(ResponseCodeEnum.EX_PARAM, key);
            }
        }
        return map;
    }

}
